<?php
//	$url="localhost";
//	$user_name="root";
//	$db_password="password";
//	$db_name="adaptive_learning";
//	$con=mysqli_connect($url, $user_name, $db_password, $db_name);
	include 'db_connect.php';

	$s_id = $_GET["matric"];

	// Check connection
	if ($con) {
		$module_array = array();
		$result = mysqli_query($con,"SELECT e.module, m.m_name FROM Enrolment e, Module m WHERE e.s_id = '" . $s_id . "' AND e.module = m.m_code");
		while($row = mysqli_fetch_array($result)) {
			$mod_code = $row['module'];
			$mod_title = $row['m_name'];
			$topic_array = array();
			$topic_result = mysqli_query($con,"SELECT * FROM Topic WHERE m_code = '" . $mod_code . "'");
			while($topic_row = mysqli_fetch_array($topic_result)) {
				$status_result = mysqli_query($con,"SELECT learning_status FROM LearningStatus WHERE s_id = '" . $s_id . "' AND m_code = '" . $mod_code . "' AND t_id = '" . $topic_row['id'] . "'");
				if ($status_row = mysqli_fetch_array($status_result)) {
					array_push($topic_array, array("topic_id" => $topic_row['id'], "topic_name" => $topic_row['t_name'], "status" => $status_row['learning_status']));
				} else {
					array_push($topic_array, array("topic_id" => $topic_row['id'], "topic_name" => $topic_row['t_name'], "status" => 'not_completed'));
				}
			}
		  $mod = array( "module_code" => $row['module'],  "module_name" => $row['m_name'], "topics" => $topic_array);
		  array_push($module_array, $mod);
		}

		echo json_encode($module_array);
	}
?>